package com.bocheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocheng.auto.JSONAuthentication;
import com.bocheng.entity.Cart;
import com.bocheng.entity.Com;
import com.bocheng.entity.Comment;
import com.bocheng.mapper.CartMapper;
import com.bocheng.mapper.ComMapper;
import com.bocheng.mapper.UserMapper;
import com.bocheng.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bocheng
 * @since 2022-03-11
 */
@RestController
@RequestMapping("/cart")
public class CartController extends JSONAuthentication {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ComMapper comMapper;

    @ApiOperation(value = "向购物车添加商品")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("comId") int  comId, @RequestParam("userId") int userId, @RequestParam("num") int  num) throws  Exception{
        Cart cart=new Cart();
        cart.setComId(comId);
        cart.setUserId(userId);
        cart.setNum(num);
        if(cartMapper.insert(cart)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除购物车商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=cartMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看购物车商品通过用户id")
    @PostMapping("/findByName")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int  userId,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("user_id",userId);
        Page<Cart> shUserPage=new Page<>(current,size);
        Page<Cart> shUserPages=cartMapper.selectPage(shUserPage,wrapper);
        List<Cart> records = shUserPages.getRecords();
        int total=cartMapper.selectCount(wrapper);
        for(Cart a:records){
            a.setUser(userMapper.selectById(a.getUserId()));
            a.setCom(comMapper.selectById(a.getComId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("product",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

